package cs.ui250.carnatic;

import java.util.HashMap;
import java.util.StringTokenizer;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Handler;

public class SoundManager {

	private static SoundManager instance = null;
	
	private SoundPool mSoundPool;
	private HashMap<Integer, Integer> mSoundPoolMap;
	private AudioManager mAudioManager;
	private Context mContext;
	private Handler handler;
	private StringTokenizer tok;
	private String currentNote;

	public static SoundManager getInstance(Context context) {
		if (instance == null) {
			instance = new SoundManager(context);
		}
		return instance;
	}
	
	private SoundManager(Context context)
	{
		mContext = context;
		mSoundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
		mSoundPoolMap = new HashMap<Integer, Integer>();
		mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
		addSound(1,R.raw.s);
		addSound(2,R.raw.r1);
		addSound(3,R.raw.r2g1);
		addSound(4,R.raw.r3g2);
		addSound(5,R.raw.g3);
		addSound(6,R.raw.m1);
		addSound(7,R.raw.m2);
		addSound(8,R.raw.p);
		addSound(9,R.raw.d1);
		addSound(10,R.raw.d2n1);
		addSound(11,R.raw.d3n2);
		addSound(12,R.raw.n3);	
	}

	public void addSound(int index, int SoundID) {
		mSoundPoolMap.put(index, mSoundPool.load(mContext, SoundID, 1));
	}

	public void playSound(int index)
	{
		int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
		mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
	}

	public void playLoopedSound(int index)
	{
		int streamVolume = mAudioManager
				.getStreamVolume(AudioManager.STREAM_MUSIC);
		mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume,1, -1, 1f);
	}
	
	public void close() {
		mSoundPool.release();
	}

	public void pause(int index) {
		mSoundPool.pause(mSoundPoolMap.get(index));
	}

	public void resume(int index) {
		mSoundPool.resume(mSoundPoolMap.get(index));
	}

	public void unload(int index) {
		mSoundPool.unload(mSoundPoolMap.get(index));
	}
	
	public void playScale(String notes) {
		handler = new Handler();
		int timeDelay = 0;
		
		tok = new StringTokenizer(notes);
		while (tok.hasMoreTokens()) {
			currentNote = tok.nextToken();
			if (currentNote.equals("S")) {
				handler.postDelayed(new Runnable(){public void run(){playSound(1);}}, timeDelay);
				timeDelay += 400;
			} else if (currentNote.equals("R1")) {
				handler.postDelayed(new Runnable(){public void run(){playSound(2);}}, timeDelay);
				timeDelay += 400;
			} else if (currentNote.equals("R2") || currentNote.equals("G1")) {
				handler.postDelayed(new Runnable(){public void run(){playSound(3);}}, timeDelay);
				timeDelay += 400;
			} else if (currentNote.equals("R3") || currentNote.equals("G2")) {
				handler.postDelayed(new Runnable(){public void run(){playSound(4);}}, timeDelay);
				timeDelay += 400;
			} else if (currentNote.equals("G3")) {
				handler.postDelayed(new Runnable(){public void run(){playSound(5);}}, timeDelay);
				timeDelay += 400;
			} else if (currentNote.equals("M1")) {
				handler.postDelayed(new Runnable(){public void run(){playSound(6);}}, timeDelay);
				timeDelay += 400;
			} else if (currentNote.equals("M2")) {
				handler.postDelayed(new Runnable(){public void run(){playSound(7);}}, timeDelay);
				timeDelay += 400;
			} else if (currentNote.equals("P")) {
				handler.postDelayed(new Runnable(){public void run(){playSound(8);}}, timeDelay);
				timeDelay += 400;
			} else if (currentNote.equals("D1")) {
				handler.postDelayed(new Runnable(){public void run(){playSound(9);}}, timeDelay);
				timeDelay += 400;
			} else if (currentNote.equals("D2") || currentNote.equals("N1")) {
				handler.postDelayed(new Runnable(){public void run(){playSound(10);}}, timeDelay);
				timeDelay += 400;
			} else if (currentNote.equals("D3") || currentNote.equals("N2")) {
				handler.postDelayed(new Runnable(){public void run(){playSound(11);}}, timeDelay);
				timeDelay += 400;
			} else if (currentNote.equals("N3")) {
				handler.postDelayed(new Runnable(){public void run(){playSound(12);}}, timeDelay);
				timeDelay += 400;
			}
		}
	}
}